package net.ashrentum.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum IntervalDataType {

	/* The order is important. From the more general type to the more specific one */
	/* There is no distinction between, for example, int or long. Only abstract types are considered */
	FLOAT("[0-9]*.[0-9]+"),
	INTEGER("[0-9]*");
	
	Pattern pattern;
	
	IntervalDataType(String pattern) {
		this.pattern = Pattern.compile(pattern);
	}
	
	static IntervalDataType getDataType(String limit) throws Exception {
		Matcher m;
		
		for (IntervalDataType dt : IntervalDataType.values()) {
			m = dt.pattern.matcher(limit);
			if (m.matches()) {
				return dt;
			}
		}
		//nothing found, then exception
		throw new Exception("Limit data type not recognized: " + limit); //TODO specific exception
	}

	static IntervalDataType getDataType(String lowerLimit, String upperLimit) throws Exception {
		IntervalDataType d1 = getDataType(lowerLimit);
		IntervalDataType d2 = getDataType(upperLimit);
		/* Get most general type */
		if (d1.ordinal() < d2.ordinal())
			return d1;
		else
			return d2;
	}
}
